package com.mdd.admin.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdd.admin.vo.ProductCategoryVo;
import com.mdd.common.entity.ProductCategory;
import com.mdd.common.mapper.ProductCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ProductCategoryUtil {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    // 获取子分类
    public List<ProductCategoryVo> getChild(Integer id) {
        List<ProductCategoryVo> productCategoryVos = new ArrayList<>();
        QueryWrapper<ProductCategory> productCategoryQueryWrapper = new QueryWrapper<>();
        productCategoryQueryWrapper.eq("parent_id", id);
        List<ProductCategory> productCategories = productCategoryMapper.selectList(productCategoryQueryWrapper);

        if (productCategories.isEmpty()) {
            return productCategoryVos;
        }

        for (ProductCategory productCategory : productCategories) {
            // 创建对应的 VO 对象
            ProductCategoryVo productCategoryVo = new ProductCategoryVo();
            productCategoryVo.setId(productCategory.getId());
            productCategoryVo.setPlate(productCategory.getPlate());
            productCategoryVo.setName(productCategory.getName());
            productCategoryVo.setParentId(productCategory.getParentId());
            productCategoryVo.setProductName(productCategory.getProductName());
            productCategoryVo.setProductType(productCategory.getProductType());
            productCategoryVo.setItemType(productCategory.getItemType());
            productCategoryVo.setCertificate(productCategory.getCertificate());
            productCategoryVo.setCertification(productCategory.getCertification());
            productCategoryVo.setProvince(productCategory.getProvince());
            productCategoryVo.setLevel(productCategory.getLevel());
            productCategoryVo.setCareerName(productCategory.getCareerName());
            productCategoryVo.setCareerDirection(productCategory.getCareerDirection());
            productCategoryVo.setIntercontinental(productCategory.getIntercontinental());
            productCategoryVo.setCountry(productCategory.getCountry());
            productCategoryVo.setPatentType(productCategory.getPatentType());
            productCategoryVo.setServerProduct(productCategory.getServerProduct());

            // 递归获取子分类
            List<ProductCategoryVo> children = this.getChild(productCategory.getId());
            productCategoryVo.setChildList(children);

            // 添加到结果列表
            productCategoryVos.add(productCategoryVo);
        }

        return productCategoryVos;
    }


}
